/**
 * Copyright (C) 2013 SHANGHAI VOLKSWAGEN, All rights reserved.
 * License version 1.0, a copy of which has been included with this.
 *
 * @File name：com.bwie.community.consumer.MessageConsumer
 * @Create on：2024/3/27
 * @Author：liuchang
 */
package com.bwie.community.consumer;

import com.bwie.common.domain.VoMessage;
import com.bwie.common.utils.MessageUtil;
import com.bwie.common.utils.MsgUtil;
import com.bwie.community.config.MqConfig;
import com.bwie.community.mapper.CommunityMapper;
import com.rabbitmq.client.Channel;
import lombok.extern.log4j.Log4j2;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * <p>ClassName：MessageConsumer</p >
 * <p>Description：</p >
 * <p>Author：liuchang</p >
 * <p>Date：2024/3/27</p >
 */
@RabbitListener(queues = MqConfig.MESSAGE_QUEUE)
@Log4j2
@Component
public class MessageConsumer {
    @Autowired
    private RedisTemplate<String,String> redisTemplate;
    @Autowired
    private CommunityMapper communityMapper;
    @Autowired
    private JavaMailSender mailSender;




    @RabbitHandler
    public void taker(Integer observeId, Message message, Channel channel) throws IOException {
        log.info("收到的消息是:{}",observeId);
        VoMessage voMessage=communityMapper.queryMessageInfo(observeId);


        String messageId = message.getMessageProperties().getMessageId();
        long deliveryTag = message.getMessageProperties().getDeliveryTag();

        redisTemplate.opsForValue().set("retry"+messageId,deliveryTag+"");

        try {
            Long add = redisTemplate.opsForSet().add("repeat" + messageId, messageId);
            if (add==1){
//                redisTemplate.opsForValue().set(voMessage.getUserPhone(), voMessage.getObserveContext());

//                SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
//                simpleMailMessage.setFrom("1312565706@qq.com");
//                simpleMailMessage.setTo(voMessage.getUserPhone());
//                simpleMailMessage.setSubject("亲,有用户评论你的圈子");
//                simpleMailMessage.setText(voMessage.getObserveContext());
//                mailSender.send(simpleMailMessage);
                String msg = MessageUtil.sendMsg(voMessage.getUserPhone());
                if (!msg.contains("成功")){
                    System.out.println("再试一次");
                    MessageUtil.sendMsg(voMessage.getUserPhone());
                }







                channel.basicAck(deliveryTag,false);
            }else {
                log.info("消息不能重复消费");
                channel.basicReject(deliveryTag,false);
            }
        } catch (IOException e) {
            String oldDeliverTag = redisTemplate.opsForValue().get("retry" + messageId);
            long old = Long.parseLong(oldDeliverTag);
            if (deliveryTag==(old+2)){
                log.info("已经重试3次,消息不能再消费");
                channel.basicNack(deliveryTag,false,false);
            }else {
                log.info("消息继续消费");
                channel.basicNack(deliveryTag,false,true);
            }
        }

    }
}
